var unitupdate = new Vue({
    el:'#unitupdate',
    //初始化数据类型
    data:{
        //修改时单个用户信息的填充
        User:{},
        text:'',
        //采购办集合信息
        optionsUser:[],
        //获取到的验证码信息
        securityCode: '',
        //用户填写的验证码
        securityCodeUser: '',
    },
    //时间提交方法
    methods:{
        //修改用户信息
        UpdateAddData:function(){
            debugger
            var _this = this;
            if(_this.User.sex=="男"){
                _this.User.sex="0"
            }else{
                _this.User.sex="1"
            }
            _this.User.address=$("#province1").val()+"-"+$("#city1").val()+"-"+$("#district1").val()+"-"+ _this.text;
            $.post("/sys/user/updateUser",{"user":JSON.stringify(_this.User)},function(data){
                if(data.data.user==1){
                    _this.$message({
                        message: '恭喜你，用户信息修改成功！',
                        type: 'success'
                    });
                }else{
                    _this.$message({
                        message: '警告哦，用户信息修改失败！',
                        type: 'warning'
                    });
                }
            });
            $("#submit-1").attr("disabled","disabled");
        },
        //获取手机验证码
        telCode:function(){
            var _this = this;
            $("#input41-1").attr("disabled","disabled");
            $.post("/home/warnInfo/sendInfo",{"tel":_this.User.telephone},function(data){
                if (data != "500") {
                    _this.securityCode = data;
                    _this.$message.success("验证码已发送!");
                    sendMessage();
                } else {
                    _this.$message.error("获取验证码失败！");
                }
            });
        },

    },
    //页面加载完成后执行此方法
    created:function(){
        var _this = this;
        debugger
        //采购单位按钮数据清空
        _this.optionsUser.length;
        _this.optionsUser=[];
        if(this.optionsUser.length == 0){
            $.post("/sys/user/seleceOfficeUser", "", function (data) {
                for (var i = 0; i < data.data.sysUser.length; i++) {
                    debugger
                    var map1 = {};
                    map1.value = data.data.sysUser[i].id;
                    map1.label = data.data.sysUser[i].name;
                    _this.optionsUser.push(map1);
                }
            });
        }
    },
});
//页面加载完成执行此程序
$(function(){
    /*$("#submit-1").attr("disabled","disabled");*/
    $("#code").attr("disabled","disabled");
    /*v_xmname();v_name();v_sex();v_telphone();v_email();v_address();v_addtext();v_createBy();*/
});
/*//提交按钮状态设置
function enableSubmit(bool){
    if(bool) {
        $("#submit-1").removeAttr("disabled");
    } else {
        $("#submit-1").attr("disabled","disabled");
    }
}*/

//验证多少项，多少个false
var flags = [false,false,false,false,false,false,false,false,false];
//flags全为true时提交按钮解除禁用
function v_submitbutton(){
    for(f in flags) if(!flags[f]) {
        enableSubmit(false);
        return;
    }
    enableSubmit(true);
}
//邮箱验证的正则
var RegEmail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
//手机号码验证
var RegTel = /^1[0-9]\d{9}$/;
//验证姓名
function v_xmname(){
    var xmname = $("#input21-1").val();
    if(xmname.trim()=="") {flags[0]=false;enableSubmit(false);
        $("#tab21-1").html("姓名不能为空");$("#img-icon21-2").css("display","block");$("#input21-1").css("border","solid 1px red");$("#tab21-1").css("display","block");}
    else{flags[0] = true;
        $("#input21-1").css("border","solid 1px #2dc12d","");$("#img-icon21-1").css("display","block");$("#tab21-1").css("display","none");$("#img-icon21-2").css("display","none");}
    v_submitbutton();
}
//验证性别
function v_sex(){
    var sex = $("#input61-1").val();
    if(sex!="男"&&sex!="女") {flags[1]=false;enableSubmit(false);
        $("#tab61-1").html("必输项");$("#img-icon61-2").css("display","block");$("#input61-1").css("border","solid 1px red");$("#tab61-1").css("display","block");}
    else{flags[1] = true;
        $("#input61-1").css("border","solid 1px #2dc12d","");$("#img-icon61-1").css("display","block");$("#tab61-1").css("display","none");$("#img-icon61-2").css("display","none");}
    v_submitbutton();

}
//验证电话
function v_telphone(){
    var telphone = $("#input41-1").val();
    if(!RegTel.test(telphone)) {flags[2]=false;enableSubmit(false);
        $("#tab41-1").html("手机号为11位");
        $("#img-icon41-2").css("display","block");
        $("#input41-1").css("border","solid 1px red");
        $("#tab41-1").css("display","block");
        $("#code").attr("disabled","disabled");
        $("#input42-1").val('')
    } else{flags[2] = true;
        $("#code").removeAttr("disabled");
        $("#input41-1").css("border","solid 1px #2dc12d","");
        $("#img-icon41-1").css("display","block");
        $("#tab41-1").css("display","none");
        $("#img-icon41-2").css("display","none");
        $("#input42-1").val('');
        $("#code").removeAttr("disabled");

    }
    v_submitbutton();

}
//验证邮箱
function v_email(){
    var email = $("#input51-1").val();
    if(!RegEmail.test(email)) {flags[3]=false;enableSubmit(false);
        $("#tab51-1").html("邮箱格式错误");$("#img-icon51-2").css("display","block");$("#input51-1").css("border","solid 1px red");$("#tab51-1").css("display","block");}
    else{flags[3] = true;
        $("#input51-1").css("border","solid 1px #2dc12d","");$("#img-icon51-1").css("display","block");$("#tab51-1").css("display","none");$("#img-icon51-2").css("display","none");}
    v_submitbutton();

}

//验证单位名称
function v_name(){
    var name = $("#input31-1").val();
    if(name.trim()==""){
        flags[4]=false;enableSubmit(false);
        $("#tab31-1").html("单位名称不能为空！");$("#img-icon31-2").css("display","block");$("#input31-1").css("border","solid 1px red");$("#tab31-1").css("display","block");
    }else{flags[4] = true;
        $("#input31-1").css("border","solid 1px #2dc12d","");$("#img-icon31-1").css("display","block");$("#tab31-1").css("display","none");$("#img-icon31-2").css("display","none");}
    v_submitbutton();
}

//验证采购区域
$(".el-input__inner").blur("on",v_createBy);
function v_createBy(){
    if($(".el-input__inner").val()==""){
        flags[5]=false;enableSubmit(false);
        $("#tabUnit-6").html("单位名称不能为空！");$("#img-iconUnit-62").css("display","block");$(".el-input__inner").css("border","solid 1px red");$("#tabUnit-6").css("display","block");
    }else{flags[5] = true;
        $(".el-input__inner").css("border","solid 1px #2dc12d","");$("#img-iconUnit-61").css("display","block");$("#tabUnit-6").css("display","none");$("#img-iconUnit-62").css("display","none");
    }
    v_submitbutton();

}

//验证地址
function v_address(){
    var selected = document.getElementsByClassName("form-control select");
    for (var i = 0; i < selected.length; i++) {
        if (selected[i].value == "") {
            flags[6] = false;
            enableSubmit(false);
            $("#tab9").html("必输项");
            $("#tab9").css("display", "block");
            $("#province1,#city1,#district1").css("border", "solid 1px red");
            $("#tab9").css("display", "block");
        } else {
            flags[6] = true;
            $("#province1,#city1,#district1").css("border", "solid 1px #2dc12d", "");
            $("#tab9").css("display", "none");
        }
        v_submitbutton();
    }
}
//验证详细地址
function v_addtext(){
    var text = $("#text").val();
    if(text.trim()=="") {flags[7]=false;enableSubmit(false);
        $("#tabUnit-8").html("必输项");$("#img-iconUnit-82").css("display","block");$("#text").css("border","solid 1px red");$("#tabUnit-8").css("display","block");}
    else{flags[7] = true;
        $("#text").css("border","solid 1px #2dc12d","");$("#img-iconUnit-81").css("display","block");$("#tabUnit-8").css("display","none");$("#img-iconUnit-82").css("display","none");}
    v_submitbutton();

}
//验证验证码
function v_code() {
    var securityCodeUser = $("#input42-1").val();
    if (securityCodeUser != unitupdate.securityCode || securityCodeUser.trim() =="") {
        flags[8] = false;
        enableSubmit(false);
        $("#tab42-1").html("请输入4位验证码");
        $("#img-icon42-2").css("display", "block");
        $("#tab42-1").css("display", "block");
        $("#input42-1").css("border", "solid 1px red");
        $("#input42-1").removeAttr("disabled");

    }
    else {
        flags[8] = true;
        $("#input42-1").css("border", "solid 1px #2dc12d", "");
        $("#img-icon42-1").css("display", "block");
        $("#tab42-1").css("display", "none");
        $("#img-icon42-2").css("display", "none");
        $("#input42-1").attr("disabled","disabled");
        $("#input41-1").attr("disabled","disabled");
        $("#code").attr("disabled","disabled");
        window.clearInterval(InterValObj);
    }
    v_submitbutton();
}
var InterValObj; //timer变量，控制时间
var count = 120; //间隔函数，1秒执行
var curCount;//当前剩余秒数

function sendMessage() {
    curCount = count;
    //设置button效果，开始计时
    $("#code").attr("disabled", "true");
    $("#code").val(curCount+"秒");
    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次

    //请求后台发送验证码 TODO

}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
        if(!flags[8]){
            $("#input41-1").removeAttr("disabled");
            $("#code").removeAttr("disabled");
        }
        $("#code").val("获取验证码");
    }
    else {
        curCount--;
        $("#code").val(curCount+"秒");
    }
}
function adaptValue(){
    return true;
}
